#ifndef _TWIT_ERROR_H_
#define _TWIT_ERROR_H_
#include "CommonMacros.h"
#include <string>
NS_BEGIN(Twit)


typedef enum ResultCode
{
   rc_unknown_error = -2,
   rc_bad_allocation = -1,
   rc_ok=0,
   /*
      1   ~ 99  : curl Error code
      200 ~ 599 : http response code (twitter Error code )
   */
   rc_twitter_error=600,
   rc_parsing_error=700,   
   rc_null_pointer_exception=1000,
   rc_invalid_argument=1001,
   rc_length_error=1002,
   rc_out_of_range=1003,
   rc_overflow_error=1004,
   rc_underflow_error=1005,
   rc_range_error=1006,
   rc_runtime_error=1007   
};

/***************************************************************************************************
   curl Error code information

   0  CURLE_OK
   1  CURLE_UNSUPPORTED_PROTOCOL
   2  CURLE_FAILED_INIT
   3  CURLE_URL_MALFORMAT
   5  CURLE_COULDNT_RESOLVE_PROXY
   6  CURLE_COULDNT_RESOLVE_HOST
   7  CURLE_COULDNT_CONNECT
   8  CURLE_FTP_WEIRD_SERVER_REPLY
   9  CURLE_REMOTE_ACCESS_DENIED    // a service was denied by the server due to lack of access - when login fails this is not returned.
   11 CURLE_FTP_WEIRD_PASS_REPLY
   13 CURLE_FTP_WEIRD_PASV_REPLY
   14 CURLE_FTP_WEIRD_227_FORMAT
   15 CURLE_FTP_CANT_GET_HOST, 
   17 CURLE_FTP_COULDNT_SET_TYPE
   18 CURLE_PARTIAL_FILE
   19 CURLE_FTP_COULDNT_RETR_FILE
   21 CURLE_QUOTE_ERROR                //quote command failure
   22 CURLE_HTTP_RETURNED_ERROR
   23 CURLE_WRITE_ERROR
   25 CURLE_UPLOAD_FAILED              //failed upload "command"
   26 CURLE_READ_ERROR                 //couldn't open/read from file
   27 CURLE_OUT_OF_MEMORY
                                      // Note: CURLE_OUT_OF_MEMORY may sometimes indicate a conversion Error
                                      //         instead of a memory allocation Error if CURL_DOES_CONVERSIONS
                                      //         is defined
   28 CURLE_OPERATION_TIMEDOUT         //the timeout time was reached
   30 CURLE_FTP_PORT_FAILED            //FTP PORT operation failed
   31 CURLE_FTP_COULDNT_USE_REST       //the REST command failed
   33 CURLE_RANGE_ERROR                //RANGE "command" didn't work
   34 CURLE_HTTP_POST_ERROR
   35 CURLE_SSL_CONNECT_ERROR          //wrong when connecting with SSL
   36 CURLE_BAD_DOWNLOAD_RESUME        //couldn't resume download
   37 CURLE_FILE_COULDNT_READ_FILE
   38 CURLE_LDAP_CANNOT_BIND
   39 CURLE_LDAP_SEARCH_FAILED
   41 CURLE_FUNCTION_NOT_FOUND
   42 CURLE_ABORTED_BY_CALLBACK
   43 CURLE_BAD_FUNCTION_ARGUMENT
   45 CURLE_INTERFACE_FAILED           //CURLOPT_INTERFACE failed
   47 CURLE_TOO_MANY_REDIRECTS         //catch endless re-direct loops 
   48 CURLE_UNKNOWN_TELNET_OPTION      //User specified an unknown option 
   49 CURLE_TELNET_OPTION_SYNTAX       //Malformed telnet option
   51 CURLE_PEER_FAILED_VERIFICATION   //peer's certificate or fingerprint
                                       //wasn't verified fine
   52 CURLE_GOT_NOTHING                //when this is a specific Error
   53 CURLE_SSL_ENGINE_NOTFOUND        //SSL crypto engine not found
   54 CURLE_SSL_ENGINE_SETFAILED       //can not set SSL crypto engine as default
   55 CURLE_SEND_ERROR                 //failed sending network data
   56 CURLE_RECV_ERROR                 //failure in receiving network data
   58 CURLE_SSL_CERTPROBLEM            //problem with the local certificate
   59 CURLE_SSL_CIPHER                 //couldn't use specified cipher
   60 CURLE_SSL_CACERT                 //problem with the CA cert (path?)
   61 CURLE_BAD_CONTENT_ENCODING       //Unrecognized transfer encoding
   62 CURLE_LDAP_INVALID_URL           //Invalid LDAP URL
   63 CURLE_FILESIZE_EXCEEDED          //Maximum file size exceeded
   64 CURLE_USE_SSL_FAILED             //Requested FTP SSL level failed
   65 CURLE_SEND_FAIL_REWIND           //Sending the data requires a rewind that failed
   66 CURLE_SSL_ENGINE_INITFAILED      //failed to initialise ENGINE
   67 CURLE_LOGIN_DENIED               //user, password or similar was not
                                       //accepted and we failed to login
   68 CURLE_TFTP_NOTFOUND              //file not found on server
   69 CURLE_TFTP_PERM                  //permission problem on server
   70 CURLE_REMOTE_DISK_FULL           //out of disk space on server
   71 CURLE_TFTP_ILLEGAL               //Illegal TFTP operation
   72 CURLE_TFTP_UNKNOWNID             //Unknown transfer ID
   73 CURLE_REMOTE_FILE_EXISTS         //File already exists
   74 CURLE_TFTP_NOSUCHUSER            //No such user
   75 CURLE_CONV_FAILED                //conversion failed
   76 CURLE_CONV_REQD                  //caller must register conversion
                                       //callbacks using curl_easy_setopt options
                                       //CURLOPT_CONV_FROM_NETWORK_FUNCTION,
                                       //CURLOPT_CONV_TO_NETWORK_FUNCTION, and
                                       //CURLOPT_CONV_FROM_UTF8_FUNCTION 
   77 CURLE_SSL_CACERT_BADFILE         //could not load CACERT file, missing or wrong format
   78 CURLE_REMOTE_FILE_NOT_FOUND      //remote file not found 
   79 CURLE_SSH                        //Error from the SSH layer, somewhat
                                       //generic so the Error getMessage will be of
                                       //interest when this has happened
   80 CURLE_SSL_SHUTDOWN_FAILED        //Failed to shut down the SSL connection
   81 CURLE_AGAIN                      //socket is not ready for send/recv,
                                       //wait till it's ready and try again (Added
                                       //in 7.18.2)
   82 CURLE_SSL_CRL_BADFILE            //could not load CRL file, missing or wrong format (Added in 7.19.0)
   83 CURLE_SSL_ISSUER_ERROR           //Issuer check failed.  (Added in 7.19.0)
   84 CURLE_FTP_PRET_FAILED            //a PRET command failed
   85 CURLE_RTSP_CSEQ_ERROR            //mismatch of RTSP CSeq numbers
   86 CURLE_RTSP_SESSION_ERROR         //mismatch of RTSP Session Identifiers
   87 CURLE_FTP_BAD_FILE_LIST          //unable to parse FTP file list
   88 CURLE_CHUNK_FAILED               //chunk callback reported Error
****************************************************************************************************/


/****************************************************************************************************

HTTP Status Codes
The Twitter API attempts to return appropriate HTTP status codes for every request. It is possible to suppress response codes for the REST API.

   200 OK: Success!
   304 Not Modified: There was no new data to return.
   400 Bad Request: The request was invalid. An accompanying Error getMessage will explain why. This is the status code will be returned during rate limiting.
   401 Unauthorized: Authentication credentials were missing or incorrect.
   403 Forbidden: The request is understood, but it has been refused. An accompanying Error getMessage will explain why. This code is used when requests are being denied due to update limits.
   404 Not Found: The URI requested is invalid or the resource requested, such as a user, does not exists.
   406 Not Acceptable: Returned by the Search API when an invalid format is specified in the request.
   420 Enhance Your Calm: Returned by the Search and Trends API when you are being rate limited.
   500 Internal Server Error: Something is broken. Please post to the group so the Twitter team can investigate.
   502 Bad Gateway: Twitter is down or being upgraded.
   503 Service Unavailable: The Twitter servers are up, but overloaded with requests. Try again later.

/****************************************************************************************************/
static const char* httpStatusCode2String(int code)
{
   switch(code)
   {
   case 200:   return "OK";
   case 304:   return "Not Modified: There was no new data to return.";
   case 400:   return "Bad Request: The request was invalid. An accompanying Error getMessage will explain why. This is the status code will be returned during rate limiting.";
   case 401:   return "Unauthorized: Authentication credentials were missing or incorrect.";
   case 403:   return "Forbidden: The request is understood, but it has been refused. An accompanying Error getMessage will explain why. This code is used when requests are being denied due to update limits.";
   case 404:   return "Not Found: The URI requested is invalid or the resource requested, such as a user, does not exists.";
   case 406:   return "Not Acceptable: Returned by the Search API when an invalid format is specified in the request.";
   case 420:   return "Enhance Your Calm: Returned by the Search and Trends API when you are being rate limited.";
   case 500:   return "Internal Server Error: Something is broken. Please post to the group so the Twitter team can investigate.";
   case 502:   return "Bad Gateway: Twitter is down or being upgraded.";
   case 503:   return "Service Unavailable: The Twitter servers are up, but overloaded with requests. Try again later.";
   }
   return "unknown Error.";
}

class Error
{
protected:
   int m_code;
   std::string m_msg;
public:
   Error():m_code(rc_ok){};
   Error(const int code, const std::string& msg):m_code(code),m_msg(msg){};
   int getCode() const { return m_code; }
   const std::string& getMessage() const
   {
      return m_msg;
   }
   const Error& getError(){ return *this; }
   inline operator const bool()
   {
      return m_code==rc_ok;
   }


   virtual const bool set(const int code, const std::string& msg)
   {
	   m_code = code;
	   m_msg = msg;
	   return m_code==rc_ok;
   }
   virtual const bool set(const int code, const char* msg)
   {
	   return set(code, std::string(msg));
   }
   virtual const bool set(const int code)
   {
	   m_code = code;
	   return bool();
   }
};

NS_END;
#endif /*_TWIT_ERROR_H_*/
